package vcp.client.GUI.mvc.model;

import java.util.List;

import vcp.client.interfaces.IObservable;
import vcp.client.request.UpdateComplaintRequest;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Complaint;
import vcp.entity.Worker;

/**
 * 
 * @author VCP team
 *         <p>
 *         complaint handler model
 *         </p>
 * @see UIAbstractModel
 */
public class UICompHandleModel extends UIAbstractModel
{
	/** worker instance **/
	private Worker customerServiceWorker;

	public Worker getCustomerServiceWorker ()
	{
		return this.customerServiceWorker;
	}

	public void setCustomerServiceWorker ( Worker worker )
	{
		this.customerServiceWorker = worker;
	}

	public UICompHandleModel ( IObservable model )
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * save the complaint
	 * 
	 * @param complaint
	 */
	public void saveComplaint ( Complaint complaint )
	{
		UpdateComplaintRequest request = new UpdateComplaintRequest(complaint);
		this.sendRequest(request, true);
	}

	/**
	 * 
	 * @param ID
	 * @return customer complaint by his id number
	 */
	public Complaint getCustomerComplaintByID ( int ID )
	{
		Complaint foundComplaint = null;
		for ( Complaint complaint : this.customerServiceWorker.getComplaints() )
		{
			if ( complaint.getID() == ID )
			{
				foundComplaint = complaint;
				break;
			}
		}
		return foundComplaint;
	}

	/**
	 * check if customer have complaint
	 * 
	 * @return true if customer has complaints
	 */
	public boolean hasComplaints ()
	{
		return ! this.customerServiceWorker.getComplaints().isEmpty();
	}

	/**
	 * 
	 * @return the first customer complaint
	 */
	public Complaint getFirstCustomerComplaint ()
	{
		return this.customerServiceWorker.getComplaints().get(0);
	}

	/**
	 * 
	 * @return list of all user complaints
	 */
	public List <Complaint> getCustomersComplaints ()
	{
		return this.customerServiceWorker.getComplaints();
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * 
	 * @param complaint
	 *            <p>
	 *            update complaint data
	 *            </p>
	 */
	private void updateComplaintInfo ( Complaint complaint )
	{
		Complaint customerComplaint = this.getCustomerComplaintByID(complaint.getID());
		if ( customerComplaint == null )
		{
			this.getCustomersComplaints().add(complaint);
		}
		else
		{
			customerComplaint.setStatus(complaint.getStatus());
			customerComplaint.setComplaintResolve(complaint.getComplaintResolve());
			customerComplaint.setAmountRefound(complaint.getAmountRefound());
			customerComplaint.setResolveText(complaint.getResolveText());
			customerComplaint.setHistory(complaint.isHistory());
			customerComplaint.setSiteEnum(complaint.getSiteEnum());
		}
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/
	@Override
	public void clearModel ()
	{
		this.customerServiceWorker = null;
	}

	@Override
	public void shouldRegisterForUpdates ()
	{
		this.model.registerForUpdates(this);
	}

	/****************************************************************************************************************************
	 *** IObserver methods ***
	 ****************************************************************************************************************************/
	@Override
	public void update ( Object data )
	{
		if ( data instanceof Complaint )
		{
			Complaint complaint = (Complaint) data;
			this.controllerDelegate.updateControllerDelegateWithData(UserNotification.COMPLAINT_UPDATED);
			this.updateComplaintInfo(complaint);

			this.controllerDelegate.updateControllerDelegateWithData(this.getCustomerComplaintByID(complaint.getID()));
			this.controllerDelegate.loadingStop();
		}
	}

	@Override
	public void updateWithReferesh ( Object data )
	{
		if ( data instanceof Complaint )
		{
			Complaint newReceivedComplaint = (Complaint) data;
			this.updateComplaintInfo(newReceivedComplaint);

			this.controllerDelegate.updateControllerDelegateWithData(newReceivedComplaint);
		}
	}
}
